package com.allianz.webapps.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.allianz.webapps.https.interfaces.appUpdateStatus.AppAfterUploadDocs;
import com.allianz.webapps.https.interfaces.type001.pojo.User;
import com.allianz.webapps.orderspojo.AppBase;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
@RequestMapping(value = "main")
public class AppAfterUploadDocsController {
	
	@Autowired
	private ObjectMapper objectMapper;
	
	@Autowired
	private AppAfterUploadDocs appAfterUploadDocs;

	@RequestMapping(value = "appAfterUplodDocs")
	@ResponseBody
	public AppBase appAfterUplodDocs(@RequestParam(value = "jsonParam") String jsonParam,HttpServletRequest request,HttpServletResponse response) throws Exception  {
		AppBase appBase=(AppBase)objectMapper.readValue(jsonParam, AppBase.class);
		appBase.setUser((User)request.getSession().getAttribute("username"));
		String body = appAfterUploadDocs.requestAfterUpload(appBase);
		JSONObject json=new JSONObject(body);
		String body1="";
		if (json.get("Body").equals(null)) {
			
			// 状态的判断有问题
			if(!("S").equals(json.getString("Status")))
				throw new Exception(json.getString("ErrMsg"));
			else
				return null;
		}else{
			body1=json.getString("Body");
			appBase=objectMapper.readValue(body1, AppBase.class);
		}
		return appBase;
	}
}
